/**
 * Created on May 13, 2007
 */
package jvs.vfs.tools;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Frame;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.net.URI;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;


/**
 * @author qiangli
 * 
 */
public class MountDialog extends BasicDialog {

	private static final long serialVersionUID = 1L;

	private JTextField uriField;

	private JComboBox optionList;
	
	private JTextField paramField;

	private JLabel error;

	private String path;

	public URI uri;

	private String[] options = new String[] { "Read","Read/Write" };
	private String[] options_value = new String[] {"r","rw"};

	private class MyFocusListener implements FocusListener {
		public void focusGained(FocusEvent e) {
			JTextField which = (JTextField) e.getSource();
			if (which == uriField) {
				error.setText("");
			}
		}

		public void focusLost(FocusEvent e) {
		}
	}

	private class OkAction extends AbstractAction {
		private static final long serialVersionUID = 1L;

		public void actionPerformed(ActionEvent ae) {

			String t = uriField.getText().trim();

			try {
				if (t.length() == 0 || t.indexOf(":") == -1) {
					throw new Exception("Valid URI required");
				}
				uri = new URI(t);
			} catch (Exception e) {
				error.setText("<html>&nbsp;&nbsp;<font color=red>"
						+ e.getMessage() + "</font><</html>");
				return;
			}

			//hide();
			setVisible(false);
			option = APPROVE_OPTION;
		}
	}

	private class CancelAction extends AbstractAction {
		private static final long serialVersionUID = 1L;

		public void actionPerformed(ActionEvent e) {
			//hide();
			setVisible(false);
			option = CANCEL_OPTION;
		}

	}

	private class BrowseAction extends AbstractAction  {
		private static final long serialVersionUID = 1L;

		public void actionPerformed(ActionEvent e) {
			JFileChooser fc = new JFileChooser();
			fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
			fc.setAcceptAllFileFilterUsed(true);
			fc.addChoosableFileFilter(new MimeTypeFileFilter(MimeTypeFileFilter.AUDIO));
			fc.addChoosableFileFilter(new MimeTypeFileFilter(MimeTypeFileFilter.IMAGE));
			fc.addChoosableFileFilter(new MimeTypeFileFilter(MimeTypeFileFilter.TEXT));
			fc.addChoosableFileFilter(new MimeTypeFileFilter(MimeTypeFileFilter.VIDEO));
			fc.addChoosableFileFilter(new MimeTypeFileFilter(MimeTypeFileFilter.APPLICATION));
			int rc = fc.showOpenDialog(getOwner());
			if (rc == JFileChooser.APPROVE_OPTION) {
				uriField.setText(fc.getSelectedFile().toURI().toString());
				uriField.grabFocus();
			}
		}
	}
	
	public MountDialog(Frame owner, String path) {
		super(owner, true);
		this.path = path;
		buildUI();
	}

	public URI getURI() {
		return uri;
	}

	public String getOptions() {
		String opl = optionList.getSelectedItem().toString();
		String param = paramField.getText().trim();
		for (int i=0; i<options.length; i++) {
			if (opl.equals(options[i])) {
				return options_value[i]+(param.length()>0?(","+param):"");
			}
		}
		return options_value[0];
	}
	
	private ImageIcon createImageIcon(String filename, String description) {
		String path = "/jvs/vfs/tools/" + filename;
		return new ImageIcon(getClass().getResource(path), description);
	}


	protected void buildUI() {
		setTitle("Mount");
		JPanel custom = new JPanel();
		custom.setLayout(new BoxLayout(custom, BoxLayout.Y_AXIS));

		JComponent[] labels = { new JLabel("Mount Point:"), new JLabel("URI:"),
				new JLabel("Options:") , new JLabel("")};
		uriField = new JTextField(32);
		uriField.setEditable(true);
		optionList = new JComboBox(options );
		paramField = new JTextField(32);
		
		JPanel uriPanel = new JPanel();
		uriPanel.setLayout(new BorderLayout());
		JButton browseButton =
			new JButton(createImageIcon("folder.gif", "Browse File"));
		browseButton.setMargin(new Insets(0, 0, 0, 0));
		browseButton.setBorderPainted(false);
		uriPanel.add(uriField, BorderLayout.CENTER);
		uriPanel.add(browseButton, BorderLayout.EAST);
		
		JTextField pathField = new JTextField(path);
		pathField.setEditable(false);
		
		JComponent[] fields = { pathField, uriPanel, optionList, paramField };

		for (int i = 0; i < fields.length; i++) {
			fields[i].setAutoscrolls(true);
		}

		JPanel panel = new JPanel();
		GridBagLayout gridbag = new GridBagLayout();
		panel.setLayout(gridbag);

		addLabelTextRows(labels, fields, gridbag, panel);

		custom.add(panel);
		custom.add(Box.createRigidArea(VGAP10));

		JPanel buttons = new JPanel();
		buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));
		buttons.add(Box.createHorizontalGlue());

		okay = new JButton("Mount");
		okay.setEnabled(true);

		buttons.add(okay);
		buttons.add(Box.createRigidArea(HGAP10));

		cancel = new JButton("Cancel");

		buttons.add(cancel);
		buttons.add(Box.createHorizontalGlue());

		buttons.setAlignmentX(Component.CENTER_ALIGNMENT);

		custom.add(buttons);
		custom.add(Box.createRigidArea(VGAP10));

		//
		Border border = BorderFactory.createEmptyBorder(10, 10, 10, 10);
		custom.setBorder(border);

		error = new JLabel("");
		getContentPane().setLayout(new BorderLayout());
		getContentPane().add(error, BorderLayout.SOUTH);
		getContentPane().add(custom, BorderLayout.CENTER);

		getRootPane().setDefaultButton(okay);

		//
		okay.addActionListener(new OkAction());
		cancel.addActionListener(new CancelAction());

		MyFocusListener fl = new MyFocusListener();
		uriField.addFocusListener(fl);
		browseButton.addActionListener(new BrowseAction());

		pack();
		
		setLocationRelativeTo(getOwner());

	}
}
